/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 050. 向下的路径节点之和
 * @author: WXY
 * @create: 2023-01-03 10:20
 * @Version 1.0
 **/
public class offer_seventeen1_pathSum {
    int ans = 0;

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) return ans;
        curNodeProcess(root, targetSum);
        pathSum(root.left, targetSum);
        pathSum(root.right, targetSum);
        return ans;
    }

    private void curNodeProcess(TreeNode root, long targetSum) {
        if (root == null) return;
        if (root.val == targetSum) ans++;
        curNodeProcess(root.left, targetSum - root.val);
        curNodeProcess(root.right, targetSum - root.val);
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
